package com.zhentao.config;


import com.zhentao.utils.AppJwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;

@Component
public class LoginFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 1、获取redquest对象以及response对象
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        if(request.getURI().getPath().contains("login")){
            return chain.filter(exchange);
        }
        // 2、在request对象里，获取token对象
        String token = request.getQueryParams().getFirst("token");
        // 3、判断token是否为null
        if (token == null) {
            // 4、如果为null，则代表用户未登录
            Result data = new Result(401, "未登录");
            return response(response,data);
        }

        try{
            Claims claimsBody = AppJwtUtil.getClaimsBody(token);
            System.err.println(claimsBody.get("id"));
        } catch (Exception e) {
            Result data = new Result(401, "token错误");
            return response(response,data);
        }
        //放行
        // 5、token有值，则代表用户登录，放行
        return chain.filter(exchange);
    }

    private Mono<Void> response(ServerHttpResponse response, Result data) {
        String jsonData = null;
        try {
            jsonData = new ObjectMapper().writeValueAsString(data);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        DataBuffer buffer =
                response.bufferFactory().wrap(jsonData.getBytes(StandardCharsets.UTF_8));
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        return response.writeWith(Mono.just(buffer));
    }

    /**
     * 过滤器的执行顺序：通过整数表示顺序，数值越小，优先级越高
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
class Result {
    private int status;
    private String msg;
    private Object data;

    public Result() {
    }

    public Result(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public Result(int status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
